package com.dormitory.javadormitory.service;

import com.dormitory.javadormitory.common.JwtTokenUtils;
import com.dormitory.javadormitory.entity.ChangePasswordDTO;
import com.dormitory.javadormitory.entity.Token;
import com.dormitory.javadormitory.entity.UserAccount;
import com.dormitory.javadormitory.exception.CustomException;
import com.dormitory.javadormitory.mapper.UserAccountMapper;
import com.dormitory.javadormitory.utils.BcryptUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAccountService {

    @Resource
    private UserAccountMapper userAccountMapper;

    //注册
    public UserAccount register(UserAccount userAccount){
        if (userAccount.getUsername()==null||"".equals(userAccount.getUsername())){
            System.out.println("账号1："+userAccount.getUsername()+"密码1："+userAccount.getPassword());
            throw new CustomException("账号不能为空");
        }
        if (userAccount.getPassword()==null||"".equals(userAccount.getPassword())){
            throw new CustomException("密码不能为空");
        }
        if (userAccount.getPassword().length() < 6 || userAccount.getPassword().length() > 16){
            throw new CustomException("密码长度必须在6到16位之间");
        }
        UserAccount userInfo = userAccountMapper.findByUsername(userAccount.getUsername());
        if (userInfo!=null){
            throw new CustomException("用户已存在");
        }
        try {
            String encodePassword = BcryptUtil.encode(userAccount.getPassword());
            System.out.println("加密后的密码："+encodePassword);
            //注入信息
            userAccount.setPassword(encodePassword);
            int rowsAffected = userAccountMapper.addUserAccount(userAccount);
            if(rowsAffected>0){
                System.out.println("用户Id: " + userAccount.getUserId());
                return userAccountMapper.findUserAccountByUserId(userAccount.getUserId());
            }else {
                return null;
            }
        }catch (Exception e){
            throw new CustomException("注册失败");
        }
    }

    //登录
    public Token login(UserAccount userAccount) {
        if (userAccount.getUsername()==null||"".equals(userAccount.getUsername())){
            throw new CustomException("账号不能为空");
        }
        if (userAccount.getPassword()==null||"".equals(userAccount.getPassword())){
            throw new CustomException("密码不能为空");
        }
        UserAccount userInfo = userAccountMapper.findByUsername(userAccount.getUsername());
        if (userInfo==null){
            throw new CustomException("账号不存在");
        }
        if (!BcryptUtil.match(userAccount.getPassword(),userInfo.getPassword())){
            throw new CustomException("账号或密码输入错误");
        }
        String strToken = JwtTokenUtils.generateToken(String.valueOf(userInfo.getUserId()));
        Token token = new Token();
        token.setToken(strToken);
        return token;
    }

    public List<UserAccount> getUserAccounts(){
        return userAccountMapper.allUserAccount();
    }


    //修改密码
    public void changePwd(ChangePasswordDTO changePasswordDTO, String userId) {
        if (changePasswordDTO.getOldPwd()==null||"".equals(changePasswordDTO.getOldPwd())){
            throw new CustomException("请输入旧密码");
        }
        if (changePasswordDTO.getNewPwd()==null||"".equals(changePasswordDTO.getNewPwd())){
            throw new CustomException("请输入新密码");
        }
        if (changePasswordDTO.getConfirmPwd()==null||"".equals(changePasswordDTO.getConfirmPwd())){
            throw new CustomException("请输入确认密码");
        }
        if ((changePasswordDTO.getNewPwd().length() < 6 || changePasswordDTO.getNewPwd().length() > 16) || (changePasswordDTO.getConfirmPwd().length() < 6 || changePasswordDTO.getConfirmPwd().length() > 16)){
            throw new CustomException("密码长度必须在6到16位之间");
        }
        UserAccount userInfo = userAccountMapper.findUserAccountByUserId(Integer.valueOf(userId));
        if (!BcryptUtil.match(changePasswordDTO.getOldPwd(),userInfo.getPassword())){
            throw new CustomException("旧密码输入错误");
        }
        if (changePasswordDTO.getOldPwd().equals(changePasswordDTO.getNewPwd())){
            throw new CustomException("新密码不能与旧密码相同");
        }
        if (!changePasswordDTO.getNewPwd().equals(changePasswordDTO.getConfirmPwd())){
            throw new CustomException("新密码和确认密码不一致");
        }
        //新密码加密
        String encodePassword = BcryptUtil.encode(changePasswordDTO.getConfirmPwd());
        changePasswordDTO.setConfirmPwd(encodePassword);
        userAccountMapper.updateUserPwdByUserId(changePasswordDTO.getConfirmPwd(),userId);



    }
}
